package com.tl.common.photo;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.media.ThumbnailUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

import com.androidquery.AQuery;
import com.tl.tlcommon.R;

public class PopupWindowPicZoom extends PopupWindow {
	Activity mContext;
	String mImageUrl;
	AQuery mAquery;
	View mView;
	ImageView mivImage;

	public PopupWindowPicZoom(Activity context, String imageUrl) {
		super(context);
		mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mView = inflater.inflate(R.layout.layout_single_picture_pop, null);

		mImageUrl = imageUrl;
		initView();
	}

	protected void initView() {
		// TODO Auto-generated method stub

		mAquery = new AQuery(mView);
		setImgUrl(mImageUrl);

		// // 首先取得屏幕对象
		// Display display = mContext.getWindowManager().getDefaultDisplay();
		// // 获取屏幕的宽和高
		// int mScreenWidth = (int) (display.getWidth() * 0.9);
		// int mScreenHeight = (int) (display.getHeight() * 0.8);

		// 设置SelectPicPopupWindow的View
		this.setContentView(mView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.Animation_ZoomLight);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mView.findViewById(R.id.pop_layout).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y > height) {
						dismiss();
					}
				}
				return true;
			}
		});

	}

	public void setImgUrl(String url) {

		mAquery.id(R.id.single_picture_pop_web)
				.progress(R.id.single_picture_pop_progress)
				.image(url, false, true);

	}

}
